﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using StudentLookup.BaseEntity;
using System.Net.Http;
using HtmlAgilityPack;
using StudentLookup.Utilities;

namespace StudentLookup.UserControl
{
    public partial class PlanExamDetail : PhoneApplicationPage
    {
        public PlanExamDetail()
        {
            InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string indexSelected = "";
            int index;
            string classId = "";
            PlanExamination planExam = new PlanExamination();
            if (NavigationContext.QueryString.TryGetValue("classId", out classId))
            {
                planExam.ClassIndependentId = classId;
            }

            string htmlString = "";
            using (var client = new HttpClient())
            {
                htmlString = await client.GetStringAsync(Constant.URL_PlanExam + planExam.ClassIndependentId);
            }
            try
            {
                HtmlDocument htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(htmlString);
                List<PlanExamination> listPlanExam = new List<PlanExamination>();
                foreach (var item in htmlDocument.DocumentNode.SelectNodes("//tr[@class='kTableRow'] | //tr[@class='kTableAltRow']"))
                {
                    planExam.CourseName = item.SelectSingleNode(".//td[3]").InnerText.Trim();
                    planExam.PlanExamDate = item.SelectSingleNode(".//td[4]").InnerText.Trim();
                    planExam.PlanExamTime = item.SelectSingleNode(".//td[5]").InnerText.Trim();
                    planExam.PlanExamTurn = item.SelectSingleNode(".//td[6]").InnerText.Trim();
                    planExam.PriorityClass = item.SelectSingleNode(".//td[7]").InnerText.Trim();
                    planExam.Faculty = item.SelectSingleNode(".//td[8]").InnerText.Trim();
                    listPlanExam.Add(planExam);
                }
                if (NavigationContext.QueryString.TryGetValue("indexSelected", out indexSelected))
                {
                    index = int.Parse(indexSelected);
                    txtClassIndependent.Text = listPlanExam[index].ClassIndependentId;
                    txtCourseName.Text = Library.LimitWords(listPlanExam[index].CourseName, 12, " ...");
                    txtPlanDate.Text = listPlanExam[index].PlanExamDate;
                    txtPlanTime.Text = listPlanExam[index].PlanExamTime;
                    txtExamTurn.Text = listPlanExam[index].PlanExamTurn;
                    txtPriorityClass.Text = listPlanExam[index].PriorityClass;
                    txtFaculty.Text = listPlanExam[index].Faculty;
                }
            }
            catch (HttpRequestException)
            {
                MessageBox.Show("Cannot connect to the QLCL. Check your internet connection");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}